package com.youlu.campus.service.order;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.entity.ActivityInfo;
import com.youlu.campus.entity.VO.req.ProducerOrderGmvReq;
import com.youlu.campus.service.cache.CacheActivityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

@Slf4j
@Service
public class ProducerOrderGmvService {


    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private CacheActivityService cacheActivityService;

    @Value("${mq.topic.orderGMV}")
    private String topic;


    @Async
    public void doHandleProducerOrderGmv(ProducerOrderGmvReq producerOrderGmvReq){
        ActivityInfo activityInfo = cacheActivityService.findById(producerOrderGmvReq.getActivityId());
        if(Objects.isNull(activityInfo)){
            return;
        }
        producerOrderGmvReq.setActivityName(activityInfo.getName());
        rocketMQTemplate.asyncSend(topic,producerOrderGmvReq, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
            }
            @Override
            public void onException(Throwable throwable) {
                log.error("统计订单 ->{}GMV消息失败",producerOrderGmvReq.getOrderNo(),throwable);
            }
        });
    }
}
